package com.voicechat.firstmet.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SnapHelper;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.adapter.IncomeRecordAdapter;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.bean.RecordListBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: IncomeRecordActivity
 * @Description: 收入明细
 * @Author: ljp
 * @CreateDate: 2018/10/11 9:50
 * @Version: 1.0
 */
public class IncomeRecordActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.recycler_income_record)
    RecyclerView recyclerIncomeRecord;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R.id.linear_layout)
    LinearLayout linearLayout;

    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String userId;
    private String token;
    private String deviceInfo;
    private int page;
    private ArrayList<RecordListBean.PageBean.ListBean> listBeans;
    private IncomeRecordAdapter incomeRecordAdapter;
    private String TAG = "IncomeRecordActivity";

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_incomerecord;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        userId = mySeleInfo.getId();
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        initView();
        initHttp();

    }

    /**
     * @return
     * @method
     * @description 收入明细
     * @date: 2018/11/7 15:47
     * @author: ljp
     */
    private void initHttp() {
        MeInfoService.incomeList(userId, token, deviceInfo, page + "").subscribe(listBean -> {
            Log.i(TAG, "initHttp: ---------"+listBean.getCode());
            if (listBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                int i = listBean.getPage().getCount() / listBean.getPage().getPageSize() +
                        (listBean.getPage().getCount() % listBean.getPage().getPageSize() == 0 ? 0 : 1);
                Log.i(TAG, "communityPostList: -------------i--------" + i);
                Log.i(TAG, "communityPostList: ----------getCount-----" + listBean.getPage().getCount());
                if (i != 0 &&page > i) {//判断是否是最后一页{//判断是否是最后一页
                    smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                } else {
                    if (page == 1) {
                        listBeans.clear();
                        smartRefresh.finishRefresh();
                    }
                    if (listBean.getPage().getList() != null) {
                        listBeans.addAll(listBean.getPage().getList());
                        incomeRecordAdapter.notifyDataSetChanged();
                        smartRefresh.setVisibility(View.VISIBLE);
                        linearLayout.setVisibility(View.GONE);
                    } else {
                        smartRefresh.setVisibility(View.GONE);
                        linearLayout.setVisibility(View.VISIBLE);
                    }
                    smartRefresh.finishLoadMore();
                }
            } else if (listBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            } else {
                toast_MakeText(listBean.getMsg());
            }
        }, throwable -> {

        });

    }

    /**
     * @return
     * @method
     * @description 布局控件实例化
     * @date: 2018/10/11 10:03
     * @author: ljp
     */
    private void initView() {
        page = 1;
        //下拉刷新
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                initHttp();
                smartRefresh.finishRefresh(10000);
            }
        });
        //上拉加载更多
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initHttp();
                smartRefresh.finishLoadMore(10000);
            }
        });
        //设置 Header 为 贝塞尔雷达 样式
        smartRefresh.setRefreshHeader(new ClassicsHeader(context));
        smartRefresh.setRefreshFooter(new ClassicsFooter(context));
        listBeans = new ArrayList<>();
        incomeRecordAdapter = new IncomeRecordAdapter(context, listBeans, R.layout.item_income_record);
        recyclerIncomeRecord.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerIncomeRecord.setAdapter(incomeRecordAdapter);
        SnapHelper snapHelper = new LinearSnapHelper();
        snapHelper.attachToRecyclerView(recyclerIncomeRecord);//这里放置的是的recycleview
    }


    @OnClick(R.id.linear_back)
    public void onClick() {
        finish();
    }

}
